﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestForm.svr;
using System.ServiceModel;
using GameBoard;
namespace TestForm
{
    public partial class Form1 : Form,IServiceCallback
    {
        public delegate void CallbackMessage(object sender, CallbackEventArgs e);
        public event CallbackMessage OnCallback;
        public event CallbackMessage OnControlEnabled;
        public event CallbackMessage OnTimer;
        public event CallbackMessage OnControlTextChanged;
        public event CallbackMessage OnControlFocus;
        public event CallbackMessage OnCheckChanged;
        public event CallbackMessage OnOwnerChanged;
        public event CallbackMessage OnUserRemove;
        public event CallbackMessage OnUserAdd;

        ServiceClient svc;
        public Form1()
        {
            svc = new ServiceClient(new InstanceContext(this));
            svc.Open();
            InitializeComponent();

            btnDice.Enabled = false;
            btnStart.Enabled = false;
            OnCallback += new CallbackMessage(CallbackHandler);
            OnControlEnabled += new CallbackMessage(ControlEnabled);
            OnTimer += new CallbackMessage(DisplayTimer);
            OnControlTextChanged += new CallbackMessage(ControlTextChanged);
            OnControlFocus += new CallbackMessage(ControlFocus);
            OnCheckChanged += new CallbackMessage(CheckChanged);
            OnOwnerChanged += new CallbackMessage(OwnerChanged);
            OnUserAdd+=new CallbackMessage(UserAdd);
            OnUserRemove+=new CallbackMessage(UserRemove);
            svc.Start();
        }

        void UserAdd(object sender, CallbackEventArgs e)
        {
            var areaControl = sender as AreaControl;
            areaControl.Users.Add(e.Message);
            areaControl.Refresh();
        }

        void UserRemove(object sender, CallbackEventArgs e)
        {
            var areaControl = sender as AreaControl;
            areaControl.Users.Remove(e.Message);
            areaControl.Refresh();
        }


        void OwnerChanged(object sender, CallbackEventArgs e)
        {
            if (sender is AreaControl)
            {
                AreaControl ac = sender as AreaControl;
                ac.Owner = e.Message;
                ac.Refresh();
            }
        }

        void CheckChanged(object sender, CallbackEventArgs e)
        {
            if (sender is CheckBox)
            {
                CheckBox chk = sender as CheckBox;
                chk.Enabled = e.Enable;
                chk.Checked = e.Check;
            }
        }

        void ControlFocus(object sender, CallbackEventArgs e)
        {
            (sender as Control).Focus();
        }

        void ControlTextChanged(object sender, CallbackEventArgs e)
        {
            Control control=sender as Control;
            if (control.Name == txtMessage.Name)
            {
                control.Text += (e.Message + Environment.NewLine);
            }
            else
            {
                control.Text = e.Message;
            }
        }

        void DisplayTimer(object sender, CallbackEventArgs e)
        {
            lblTimer.Text = e.Count.ToString();
        }
        void ControlEnabled(object sender, CallbackEventArgs e)
        {
            Control control = sender as Control;
            control.Enabled = e.Enable;
            control.Focus();
        }
        
        void CallbackHandler(object sender, CallbackEventArgs e)
        {
            txtMessage.Text += e.Message + Environment.NewLine;
        }

        private void btnDice_Click(object sender, EventArgs e)
        {
            svc.RollDice();
            
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            svc.Login(txtId.Text);
        }

        #region IServiceCallback Members

        public void GameBoardOwnerChanged(string EstateTitle, string owner)
        {
            if (Controls.ContainsKey(EstateTitle))
            {
                OnOwnerChanged(Controls[EstateTitle], new CallbackEventArgs(owner));
            }
        }
        public void GameBoardPlayerRemove(string AreaTitle, string playerName)
        {
            if (Controls.ContainsKey(AreaTitle))
            {
                OnUserRemove(Controls[AreaTitle], new CallbackEventArgs(playerName));      
            }
        }
        public void GameBoardPlayerDisplay(string AreaTitle, string playerName)
        {
            if (Controls.ContainsKey(AreaTitle))
            {
                OnUserAdd(Controls[AreaTitle], new CallbackEventArgs(playerName));
            }
        }

        public void Message(string message1)
        {
            OnCallback(this, new CallbackEventArgs(message1));
        }
        public void ControlEnable(string name,bool enable)
        {
            if (Controls.ContainsKey(name))
            {
                OnControlEnabled(Controls[name], new CallbackEventArgs(enable));
            }
        }
        public void ControlTextChanged(string name, string content)
        {
            if (Controls.ContainsKey(name))
            {
                OnControlTextChanged(Controls[name],new CallbackEventArgs(content));
            }
        }
        public void ControlFocus(string name)
        {
            if (Controls.ContainsKey(name))
            {
                OnControlFocus(Controls[name], new CallbackEventArgs(name));
            }
        }
        public void CheckChanged(string name,bool enable,bool check)
        {
            if (Controls.ContainsKey(name))
            {
                OnCheckChanged(Controls[name], new CallbackEventArgs(enable, check));
            }
        }

        public void TimerCount(int count)
        {
            OnTimer(this, new CallbackEventArgs(count));    
        }
        
        
        
        
        #endregion

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            svc.Quit();
            svc.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            svc.InitGame();
        }

        private void btnBuyEstate_Click(object sender, EventArgs e)
        {
            svc.Buy(txtArea.Text);
        }

        private void btnSellEstate_Click(object sender, EventArgs e)
        {
            svc.Sell(txtArea.Text);
        }

        private void chkInn_Click(object sender, EventArgs e)
        {
            if (chkInn.Checked)
            {
                svc.BuyLodging(txtArea.Text, Lodging.Inn);
            }
            else
            {
                svc.SellLodging(txtArea.Text, Lodging.Inn);
            }
        }

        private void chkCondo_Click(object sender, EventArgs e)
        {
            if (chkCondo.Checked)
            {
                svc.BuyLodging(txtArea.Text, Lodging.Condo);
            }
            else
            {
                svc.SellLodging(txtArea.Text, Lodging.Condo);
            }
        }

        private void chkHotel_Click(object sender, EventArgs e)
        {
            if (chkHotel.Checked)
            {
                svc.BuyLodging(txtArea.Text, Lodging.Hotel);
            }
            else
            {
                svc.SellLodging(txtArea.Text, Lodging.Hotel);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        
    }
    public class CallbackEventArgs : EventArgs
    {
        public string Message
        {
            private set;
            get;
        }
        public int Count
        {
            private set;
            get;
        }
        public bool Enable
        {
            private set;
            get;
        }
        public bool Check
        {
            private set;
            get;
        }

        public CallbackEventArgs(string message)
            :base()
        {
            Message = message;
        }
        public CallbackEventArgs(bool enable)
        {
            Enable = enable;
        }
        public CallbackEventArgs(int count)
            :base()
        {
            Count = count;
        }
        public CallbackEventArgs(bool enable, bool check)
        {
            Enable = enable;
            Check = check;
        }
    }
}
